package iss.team2.lams.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import iss.team2.lams.dao.LeaveTypeDAOImpl;
import iss.team2.lams.dao.PublicHolidayDAOImpl;
import iss.team2.lams.model.LeaveType;
import iss.team2.lams.model.PublicHoliday;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class AdminControllerM 
{
	@Autowired
	LeaveTypeDAOImpl lti;
	
	@Autowired
	PublicHolidayDAOImpl phi;
	
	//Admin Navigation
	
	@RequestMapping(value = "/admin", method = RequestMethod.GET)
	public ModelAndView adminView()
	{
		return new ModelAndView("adminView");
	}
	
	@RequestMapping(value = "/admin/managestaff", method = RequestMethod.GET)
	public ModelAndView managestaff()
	{
		return new ModelAndView("admin_createStaff");
	}
	@RequestMapping(value = "/admin/manage-entitlement", method = RequestMethod.GET)
	public ModelAndView managent()
	{
		return new ModelAndView("admin_manageEntitlement");
	}
	//Admin-Logi Flow
//	@RequestMapping(value = "/admin/create", method = RequestMethod.POST, params="save")
//	public ModelAndView submitEmp(@ModelAttribute Employee e, final RedirectAttributes rda)
//	{
//		ModelAndView mav=new ModelAndView();
//		System.out.println("");
//		empservice.create(e);
//		String message="New Employee " + e.getName()+ " was successfully created.";
//		System.out.println("New Employee was created");
//		rda.addFlashAttribute("message", message);
//		return mav;
//	}
//	@RequestMapping(value = "/admin/manage-publicHoliday", method = RequestMethod.POST, params="update")
//	public String addStudent(@ModelAttribute("SpringWeb") PublicHoliday ph,  ModelMap model)
//	{
//		model.addAttribute("pid", ph.getPhId());
//		model.addAttribute("desc", ph.getDescription());
//		return "TEST";
//		
//		
//	}
//	@RequestMapping(value = "/TEST", method = RequestMethod.GET)
//	public ModelAndView test()
//	{
//		return new ModelAndView("TESTM");
//	}
	@RequestMapping(value = "/admin/manageleavetype", method = RequestMethod.GET)
	public ModelAndView vieLeaveType()
	{
		return new ModelAndView("admin_leaveType");
	}
	
	//Just For Testing
//	@RequestMapping(value = "/admin/leavetype", method = RequestMethod.POST, params="save")
//	public String enterLeaveType(@ModelAttribute("SpringWeb")LeaveType leavetype, ModelMap model)
//	{
//		model.addAttribute("leave_code", leavetype.getLeaveCode());
//		model.addAttribute("leave_type", leavetype.getLeaveType());
//		model.addAttribute("leave_desc", leavetype.getDescription());
//		model.addAttribute("leave_number", leavetype.getNumOfDay());
//		return "TESTM";
//	}
	@RequestMapping(value = "/admin/manage-publicHoliday", method = RequestMethod.GET)
	public ModelAndView viewPublicHoliday()
	{
		return new ModelAndView("admin_recordHoliday");
	}
	@RequestMapping(value="/admin/publicHoliday",method=RequestMethod.POST,params="update")
	public void enterLeaveType(@ModelAttribute("SpringWeb")PublicHoliday ph, ModelMap model, WebRequest webRequest)
	{
		String l_dateStr = webRequest.getParameter("dtpHoliday");
		try {
			SimpleDateFormat l_fmt = new SimpleDateFormat("yyyy-MM-dd");
			Date l_date = l_fmt.parse(l_dateStr);
			ph.setDate(l_date);
		} catch (Exception e) {
			// TODO: handle exception
		}
//		model.addAttribute("phId", ph.getPhId());
//		model.addAttribute("date", ph.getDate());
//		model.addAttribute("desc", ph.getDescription());
//		return "TESTG";
		phi.create(ph);
		
	}
	@RequestMapping(value = "/admin/leavetype", method = RequestMethod.POST, params="save")
	public void createLeaveType(@ModelAttribute("SpringWeb")LeaveType leavetype, ModelMap model)
	{
		lti.create(leavetype);
		
		
		
		
	}
	
	
	
	
	
	
	
	
	
	

}
